/*
 * N900Summary.cpp
 *
 *  Created on: Aug 30, 2016
 *      Author: ubuntu
 */

#include "N900Summary.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "../common.h"
#include "../Conf.h"

using namespace std;

N900Summary::N900Summary() : Summary()
{
	memset(&msg, 0, sizeof(msg));
	m_pbuf = NULL;
	m_len = 0;
}

N900Summary::~N900Summary()
{
	n900_msg_free(&msg);
	if (m_pbuf)
		free(m_pbuf);
}

void N900Summary::getBuf(unsigned char *buf, size_t len)
{
	m_pbuf = buf;
	m_len = len;
}

void N900Summary::parse()
{
	int min_len = DM_TAG_HEAD_LEN + 5;
	if (!m_pbuf || m_len <= min_len) {
		debug(LOG_DEBUG, "是吗");
		return;
	}
	unsigned char *p = m_pbuf + min_len;
	size_t nLen = m_len - min_len;

	int pos = DM_TAG_HEAD_LEN+1;

	msg.curtime = m_pbuf[pos] << 24 | m_pbuf[pos+1] << 16 | m_pbuf[pos+2] << 8 | m_pbuf[pos+3];

#if 0
	free(m_pbuf);
	m_pbuf = NULL;
#endif

	n900_parse_package(p, nLen, &msg);
}

void N900Summary::writeDB(Connection *pConn)
{
	if (msg.result == false)
	{
#if 0
		char *tmp = (char *)malloc(m_len * 2 + 1);
		if (tmp) {
			for (size_t i = 0; i < m_len; i++) {
				sprintf(tmp+i*2, "%02X", tmp[i]);
			}
			tmp[m_len*2] = 0;
			free(tmp);
		}
#endif
		debug(LOG_ERR, "Parse error %d", msg.type);
		return;
	}
	if (msg.obdid == 0) 
	{
		debug(LOG_ERR, "OBD error, return");
		return;
	}
	switch (msg.type) {
	case N900_CARDATA:
		{
			Query query = pConn->query();
			query << "INSERT INTO zb_n900_cardata_" << dateftime(msg.msg_time, "%Y%m%d") << 
				"(obdid, curtime, time, avgspeed, maxspeed, minspeed, fuel_consumption, mileage, sec_cnt, drive_cnt) VALUES(" <<
				msg.obdid << "," << quote << dateftime(msg.curtime, "%F %H:%M:%S") << "," << quote << dateftime(msg.msg_time, "%F %H:%M:%S") << "," << msg.m_avgSpeed << "," << msg.m_maxSpeed << "," << msg.m_minSpeed << "," << 
				setprecision(3) << setiosflags(ios::fixed) << msg.m_fuleConsumptionTotal << "," <<
				setprecision(3) << setiosflags(ios::fixed) << msg.m_mileageTotal << "," << msg.m_sectionDataCount << "," << msg.m_driveActionCount << ")";
		//	debug(LOG_DEBUG, "Query str: %s", query.str().c_str());
			query.exec();

		ulonglong id = query.insert_id();
		if (msg.m_sectionDataCount > 0 && msg.m_sec) {
			for (int i = 0; i < msg.m_sectionDataCount; i++) {
				query << "INSERT INTO zb_n900_cardata_subsection_" << dateftime(msg.msg_time, "%Y%m%d") << 
					"(subid, sub_standard, time, mileage, fuel_consumption) VALUES(" <<
					id << "," << msg.m_sec[i].id << "," << msg.m_sec[i].stime << "," 
					<< setprecision(3) << setiosflags(ios::fixed) << msg.m_sec[i].mileage << "," << setprecision(3) << setiosflags(ios::fixed) << msg.m_sec[i].fuelConsumption << ")";
				query.exec();
			}
		}

		if (msg.m_driveActionCount > 0 && msg.m_drive) {
			for (int i = 0; i < msg.m_driveActionCount; i++) {
			}
		}
		}
		break;
	case N900_GPSDATA:
		break;
	case N900_REPORT:
		break;
	case N900_BEATDATA:
		break;
	case N900_FILETRANS:
		break;
	default:
		debug(LOG_DEBUG, "什么种类: %d", msg.type);
		break;
	}
}

void N900Summary::createTB(Connection *pConn)
{
}

